<template>
  <div class="user_wrap">
    <BreadCrumb crumb="sku管理"></BreadCrumb>
    <div class="top_bar">
      <el-row>
        <el-col :span="8">
          <el-input
            placeholder="请输入搜索内容"
            v-model="keyword"
            class="input-with-select"
            size="small"
          >
            <el-button
              slot="append"
              icon="el-icon-search"
              @click="fnGetData(1)"
            ></el-button>
          </el-input>
        </el-col>
        <el-col :span="8" :offset="8"
          ><AddSku @fnResetTable="fnGetData" class="pull-right"></AddSku
        ></el-col>
      </el-row>
    </div>
    <SkuTable :skus="aSkuList" @fnResetTable="fnGetData(1)"></SkuTable>
    <el-pagination
      background
      layout="prev, pager, next"
      :total="total"
      :current-page="page"
      :page-size="pagesize"
      style="text-align: center; margin-top: 10px"
      @current-change="fnGetPage"
    >
    </el-pagination>
  </div>
</template>

<script>
import BreadCrumb from "@/components/widget/BreadCrumb";
import AddSku from "@/components/widget/AddSku";
import SkuTable from "@/components/widget/SkuTable";
import cons from "@/components/constant";

export default {
  name: "Sku",
  data() {
    return {
      keyword: "",
      page: 1,
      pagesize: 3,
      aSkuList: [],
      total: 0,
      pop_show: false,
    };
  },
  components: {
    BreadCrumb,
    AddSku,
    SkuTable,
  },
  mounted() {
    this.fnGetData(1);
  },
  methods: {
    // 获取所有的数据
    fnGetData: function (num) {
      let token = localStorage.token;

      // 请求后端的接口
      this.axios
        .get(cons.apis + "/goods/skus/", {
          headers: {
            Authorization: "JWT " + token,
          },
          responseType: "json",
          params: {
            page: num, // 第几页数据
            pagesize: this.pagesize, // 每页显示的条数
            keyword: this.keyword,
          },
        })
        .then((dat) => {
          console.log("加载所有的sku的响应>>>", dat);
          this.aSkuList = dat.data.results;
          // 获取所有数据
          this.total = dat.data.count;
        })
        .catch((err) => {
          console.log(err);
        });
    },
    // 点击添加时触发的方法
    fnGetKey: function (dat) {
      this.keyword = dat;
      this.fnGetData(1);
    },
    // 切换分页时触发的方法
    fnGetPage: function (dat) {
      console.log("dat>>", dat);
      this.page = dat;
      this.fnGetData(this.page);
    },
  },
};
</script>

<style scoped>
.top_bar {
  width: 95.2%;
  overflow: hidden;
  margin: 10px auto 10px;
}
</style>
